<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="user_type" placeholder="请选择类"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="用户名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['username', validatorRules.username]" placeholder="请输入用户名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="企业名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输企业名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入手机号/法人手机号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['gender']" :trigger-change="true" dictCode="sex" placeholder="请选择性别"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idNo']" placeholder="请输入身份证号/法人身份证"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="统一社会信用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unionCredit']" placeholder="请输入统一社会信用"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="法人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['legalName']" placeholder="请输入法人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['email']" placeholder="请输入邮箱"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['earnestMoney']" placeholder="" style="width: 100%" :min="0" disabled/>
            </a-form-item>
          </a-col>
          <!--<a-col :span="8">
            <a-form-item label="冻结保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['frozenEarnest']" placeholder="" style="width: 100%" :min="0" disabled/>
            </a-form-item>
          </a-col>-->
          <a-col :span="8">
            <a-form-item label="是否有效" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isAvailable']" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否有效"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="是否会员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isMember']" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否会员"/>
            </a-form-item>
          </a-col>

          <a-col :span="8">
            <a-form-item label="身份证照片正面" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['shenfenzhengzhengmianUrl']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="身份证照片反面" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['shenfenzhengfanmianUrl']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="营业执照" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['yingyezhizhaoUrl']"></j-image-upload>
            </a-form-item>
          </a-col>

          <a-col :span="8">
            <a-form-item label="入会申请书" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--              <j-image-upload isMultiple  v-decorator="['ruhuishenqingUrl']"></j-image-upload>-->
              <a-upload
                name="file"
                :multiple="true"
                :file-list="fileList">
                <a-button> <a-icon type="upload" /> 选择入会材料模板 </a-button>
              </a-upload>
            </a-form-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-item label="申请状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['auditState']" :trigger-change="true" dictCode="audit_state" placeholder="请选择申请状态"/>
            </a-form-item>
          </a-col>-->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: 'CustomerForm',
    components: {
      JFormContainer,
      JDictSelectTag,
      JImageUpload
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        fileList:[],
        labelCol: {
          /*xs: { span: 24 },
          sm: { span: 5 },*/
          span:8
        },
        wrapperCol: {
          /*xs: { span: 24 },
          sm: { span: 16 },*/
          span:16
        },
        confirmLoading: false,
        validatorRules: {
          type: {
            rules: [
              { required: false, message: '请输入类别!'},
            ]
          },
          username: {
            rules: [
              { required: false, message: '请输入用户名!'},
            ]
          },
          name: {
            rules: [
              { required: false, message: '请输入姓名/名称!'},
            ]
          },
          phone: {
            rules: [
              { required: false, message: '请输入手机号/法人手机号!'},
            ]
          },
        },
        url: {
          add: "/customer/add",
          edit: "/customer/edit",
          queryById: "/customer/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        /*
        * 这里是附件显示  必须符合数组对象规范  要包含 uid  name  status  url
        *
        *
        * */
        let fileList = []
        for (let recordKey in record.ruhuishenqingUrl) {
          let name = record.ruhuishenqingUrl[recordKey]['url'].split('/')
          let name1 = name[name.length-1]
          // console.log(name)
          fileList.push({
            uid:recordKey,
            name:name1,
            status: 'done',
            url: record.ruhuishenqingUrl[recordKey]['url'],
          })
        }
        this.fileList = fileList,
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'type','username','name','phone','gender','idNo','unionCredit','legalName','email','earnestMoney','isAvailable','isMember','shenfenzhengzhengmianUrl','shenfenzhengfanmianUrl','yingyezhizhaoUrl'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'type','username','name','phone','gender','frozenEarnest','idNo','unionCredit','legalName','email','earnestMoney','isAvailable','isMember'))
      },
    }
  }
</script>